package com.software.questioncenter.controller;

import com.software.questioncenter.domain.Result;
import com.software.questioncenter.entity.Answer;
import com.software.questioncenter.entity.Question;
import com.software.questioncenter.exception.BizException;
import com.software.questioncenter.service.*;
import com.software.questioncenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: wjx
 * Description: 题库的控制类
 */
@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;

    //新增题目
    @PostMapping("/createQuestion")
    public Result createQuestion(@RequestBody Question question){
        String result = questionService.createQuestion(question);
        return ResultUtil.success("200", result, null);
    }

    //删除题目
    @PostMapping("/deleteQuestion")
    public Result deleteQuestion(@RequestBody Question question){
        String result = questionService.deleteQuestion(question.getId());
        return ResultUtil.success("200", result, null);
    }


    //修改题目
    //无论修改什么信息，会将数据库的信息全部修改成question
    @PostMapping("/updateQuestion")
    public Result updateQuestion(@RequestBody Question question){
        String result = questionService.updateQuestion(question);
        return ResultUtil.success("200", result, null);
    }

    //查看所有题目（分页）
    @GetMapping("/queryQuestionList")
    public Result queryAll(@RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                  @RequestParam(name="size",required = false,defaultValue = "10")Integer size){
        if(page<1){
            throw new BizException("【查看题目】:当前页码不能小于1");
        }
        /*//按照题库id排序  降序排列 后提交的题目在前边
        Sort.Order order = Sort.Order.desc("id");
        Sort sort = Sort.by(order);*/
        //都是从0开始 所以page-1
        Pageable pageable = PageRequest.of(page-1, size);
        Page pageResult = questionService.findAll(pageable);
        return ResultUtil.success("200", "查询所有题目成功", pageResult);
    }

    //新增某个题的题解
    @PostMapping("/publishAnswer")
    public Result createAnswer(@RequestBody Answer answer){
        answerService.createAnswer(answer);
        return ResultUtil.success("200", "发布题解成功", answer);
    }

    //查看题目详情
    @GetMapping("/findQuestion/{questionId}")
    public Result findById(@PathVariable Integer questionId){
        Question question = questionService.findById(questionId);
        return ResultUtil.success("200", "查询成功", question);
    }

    //查看题解列表（分页）
    @GetMapping("/queryAnswerList")
    public Result queryAnswerList(@RequestParam String questionId,@RequestParam Integer page,@RequestParam Integer size){
        if(page<1){
            throw new BizException("【查看题解列表】:当前页码不能小于1");
        }
        //按照答案id排序  降序排列 最新提交的答案在前边
        Sort.Order order = Sort.Order.desc("id");
        Sort sort = Sort.by(order);
        Pageable pageable=PageRequest.of(page-1,size,sort);
        Page pageResult = answerService.queryAnswerList(questionId, pageable);
        return ResultUtil.success("200","查询题解列表成功",pageResult);
    }

    //根据内容查询题解
    @GetMapping("/queryAnswerByContent")
    public Result queryAnswerByContent(@RequestParam String content, @RequestParam Integer questionId){
        List<Answer> answers = answerService.queryAnswerByContent(content, questionId);
        if(answers.size()!=0){
            return ResultUtil.success("200","根据内容查询题解成功",answers);
        }
        return ResultUtil.error("500","不存在与查询内容相关的题解",null);
    }
    //根据题解AnswerId具体查询题解
    @GetMapping("/queryAnswer/{answerId}")
    public Result queryAnswer(@PathVariable String answerId){
        Answer answer = answerService.findById(answerId);
        return ResultUtil.success("200","查看具体题解成功",answer);
    }

    //查看题目列表
    //根据难度查询题目（分页）
    @GetMapping("/findDegree/{degree}")
    public Result findByDegree(@PathVariable String degree,
                                  @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                  @RequestParam(name="size",required = false,defaultValue = "10")Integer size){
        if(page<1){
            throw new BizException("【查看题目列表】:当前页码不能小于1");
        }
        //都是从0开始 所以page-1
        Pageable pageable=PageRequest.of(page-1,size);
        Page pageResult=questionService.findByDegree(degree, pageable);
        return ResultUtil.success("200","查询题目成功",pageResult);
    }

    //查看题目列表
    //根据标题模糊查询（分页）
    @GetMapping("/findTitle/{title}")
    public Result findByName(@PathVariable String title,
                                       @RequestParam(name="page",required = false,defaultValue = "1") Integer page,
                                       @RequestParam(name="size",required = false,defaultValue = "10")Integer size){
        if(page<1){
            throw new BizException("【查看题目列表】:当前页码不能小于1");
        }
        //都是从0开始 所以page-1
        Pageable pageable=PageRequest.of(page-1,size);
        Page pageResult=questionService.findByName(title, pageable);
        return ResultUtil.success("200","查询题目成功",pageResult);
    }

    @GetMapping("/findQuestionNums")
    public Result findQuestionNums(){
        Integer questionNums = questionService.findQuestionNums();
        return ResultUtil.success("200","查询题目数量成功",questionNums);
    }
}
